//
//  M1RangeSlider.m
//  NewMarryU
//
//  Created by Cedar on 2018/7/17.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1RangeSlider.h"
#import "M1RangeSliderKnobLayer.h"
#import "M1RangeSliderTrackLayer.h"

@interface M1RangeSlider ()
{
    M1RangeSliderTrackLayer* _m1_trackLayer;
    M1RangeSliderKnobLayer* _m1_upperKnobLayer;//slider左边的滑块
    M1RangeSliderKnobLayer* _m1_lowerKnobLayer;//slider右边的滑块
    
    float _m1_knobWidth;
    float _m1_useableTrackLength;
    CGPoint _m1_previousTouchPoint;
}

@end

@implementation M1RangeSlider

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _m1_maximumValue = 60.0;
        _m1_minimumValue = 18.0;
        
        _m1_upperValue = 26.0;
        _m1_lowerValue = 18.0;
        
        _m1_trackHighlightColour = [UIColor blackColor];
        _m1_trackColour = m1_color(0xF7F9FB);
        _m1_knobColour = [UIColor whiteColor];
        _m1_curvaceousness = 1.0;
        
        _m1_trackLayer = [M1RangeSliderTrackLayer layer];
        _m1_trackLayer.m1_slider = self;
        [self.layer addSublayer:_m1_trackLayer];
        
        _m1_upperKnobLayer = [M1RangeSliderKnobLayer layer];
        _m1_upperKnobLayer.m1_slider = self;
        [self.layer addSublayer:_m1_upperKnobLayer];
        
        _m1_lowerKnobLayer = [M1RangeSliderKnobLayer layer];
        _m1_lowerKnobLayer.m1_slider = self;
        [self.layer addSublayer:_m1_lowerKnobLayer];
        
        [self setLayerFrames];
    }
    return self;
}

- (void) setLayerFrames
{
    CGRect rect = self.bounds;
    rect.size.height = 16;
    rect.origin.y = 2;
    _m1_trackLayer.frame = CGRectInset(rect, 0, self.bounds.size.height / 3.5);
    [_m1_trackLayer setNeedsDisplay];
    
    _m1_knobWidth = self.bounds.size.height;
    _m1_useableTrackLength = self.bounds.size.width - _m1_knobWidth;
    
    //现根据滑块对应的的值计算滑块center的x值，然后设置滑块的frame
    float upperKnobCentre = [self m1_positionForValue:_m1_upperValue];
    _m1_upperKnobLayer.frame = CGRectMake(upperKnobCentre - _m1_knobWidth / 2, 0, _m1_knobWidth, _m1_knobWidth);
    
    float lowerKnobCentre = [self m1_positionForValue:_m1_lowerValue];
    _m1_lowerKnobLayer.frame = CGRectMake(lowerKnobCentre - _m1_knobWidth / 2, 0, _m1_knobWidth, _m1_knobWidth);
    
    [_m1_upperKnobLayer setNeedsDisplay];
    [_m1_lowerKnobLayer setNeedsDisplay];
}

//根据滑块对应的值来计算滑块center的x的值
- (float) m1_positionForValue:(float)value
{
    return _m1_useableTrackLength * (value - _m1_minimumValue) /
    (_m1_maximumValue - _m1_minimumValue) + (_m1_knobWidth / 2);
}

- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
    _m1_previousTouchPoint = [touch locationInView:self];
    
    // hit test the knob layers
    //判断是否触摸到了滑块
    if(CGRectContainsPoint(_m1_lowerKnobLayer.frame, _m1_previousTouchPoint))
    {
        _m1_lowerKnobLayer.m1_highlighted = YES;
        [_m1_lowerKnobLayer setNeedsDisplay];
    }
    else if(CGRectContainsPoint(_m1_upperKnobLayer.frame, _m1_previousTouchPoint))
    {
        _m1_upperKnobLayer.m1_highlighted = YES;
        [_m1_upperKnobLayer setNeedsDisplay];
    }
    return _m1_upperKnobLayer.m1_highlighted || _m1_lowerKnobLayer.m1_highlighted;
}

#define BOUND(VALUE, UPPER, LOWER)  MIN(MAX(VALUE, LOWER), UPPER)

- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [touch locationInView:self];
    
    // 1. determine by how much the user has dragged
    float delta = touchPoint.x - _m1_previousTouchPoint.x;
    float valueDelta = (_m1_maximumValue - _m1_minimumValue) * delta / _m1_useableTrackLength;
    
    _m1_previousTouchPoint = touchPoint;
    
    // 2. update the values
    if (_m1_lowerKnobLayer.m1_highlighted)
    {
        _m1_lowerValue += valueDelta;
        _m1_lowerValue = BOUND(_m1_lowerValue, _m1_upperValue, _m1_minimumValue);
    }
    if (_m1_upperKnobLayer.m1_highlighted)
    {
        _m1_upperValue += valueDelta;
        _m1_upperValue = BOUND(_m1_upperValue, _m1_maximumValue, _m1_lowerValue);
    }
    
    // 3. Update the UI state
    [CATransaction begin];
    [CATransaction setDisableActions:YES] ;
    
    [self setLayerFrames];
    
    [CATransaction commit];
    
    [self sendActionsForControlEvents:UIControlEventValueChanged];
    
    return YES;
}

- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
    _m1_lowerKnobLayer.m1_highlighted = _m1_upperKnobLayer.m1_highlighted = NO;
    [_m1_lowerKnobLayer setNeedsDisplay];
    [_m1_upperKnobLayer setNeedsDisplay];
}



@end
